/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;

import net.openarmory.core.config.ConfigurationProperties;
import net.openarmory.core.domain.constants.ArmoryZone;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
 * The Class {@code ArmoryXmlWriter} is a helper class for writing XML data to
 * disk.
 */
class ArmoryXmlWriter {
	/** The log. */
	private static final Logger LOG = Logger.getLogger(ArmoryXmlWriter.class);

	/** The configuration properties. */
	private ConfigurationProperties config;

	/**
	 * Instantiates a new armory XML writer.
	 */
	public ArmoryXmlWriter() {
		// retrieve configuration
		try {
			this.config = ConfigurationProperties.getInstance();
		} catch (final IOException e) {
			LOG.error("Can not open config file.", e);
		}
	}

	/**
	 * Creates the file where the XML will be written to.
	 * 
	 * @param outFilename
	 *            The name of the file.
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param realm
	 *            The realm on which the character resides on.
	 * @param specified
	 *            The guild or character name.
	 * @param characterSpecific
	 *            This {@code boolean} that specified if the file is character
	 *            specific.
	 * 
	 * @return The output file.
	 */
	protected File createOutFile(final String outFilename,
			final ArmoryZone zone, final String realm, final String specified,
			final boolean characterSpecific) {
		File outFile = null;
		if (characterSpecific) {
			outFile = new File(this.config.getOutputDir() + File.separatorChar
					+ zone + File.separatorChar + realm + File.separatorChar
					+ specified + File.separatorChar + outFilename);
		} else {
			outFile = new File(this.config.getOutputDir() + File.separatorChar
					+ "items" + File.separatorChar + outFilename);
		}
		return outFile;
	}

	/**
	 * Write the XML file to disk.
	 * 
	 * @param xmlStream
	 *            The {@code InputStream} that contains the XML data.
	 * @param outFile
	 *            The output file.
	 */
	protected void writeXmlFile(final InputStream xmlStream, final File outFile) {
		final BufferedReader in = new BufferedReader(new InputStreamReader(
				xmlStream));
		final Collection<String> lines = new ArrayList<String>();
		String inputLine;
		try {
			while ((inputLine = in.readLine()) != null) {
				lines.add(inputLine);
				LOG.trace(inputLine);
			}

			in.close();

			FileUtils.writeLines(outFile, "UTF-8", lines);
			LOG.info("File written to: " + outFile.getAbsolutePath());
		} catch (final IOException e) {
			LOG.error("IOException", e);
		}
	}
}
